package com.wyix;

import com.wyix.entity.Course;
import com.wyix.service.CourseManager;

/**
 * @author wangdx
 * @since 2025/8/5
 */
public class CourseTest {
    public static void main(String[] args) throws InterruptedException {
        CourseManager manager = new CourseManager();
        // 初始化 1 门热门课程（剩余 3 个名额）
        manager.initCourse(new Course("CS101", "Java编程", 3));

        // 模拟 10 个学生同时选课（10 个线程并发操作）
        int studentCount = 10;
        Thread[] students = new Thread[studentCount];
        for (int i = 0; i < studentCount; i++) {
            students[i] = new Thread(() -> {
                boolean success = manager.selectCourse("CS101");
                String result = success ? "✅ 选课成功" : "❌ 选课失败（名额已满）";
                System.out.println(Thread.currentThread().getName() + ": " + result);
            }, "学生" + (i + 1));
        }

        // 启动所有线程
        for (Thread t : students) {
            t.start();
        }

        // 等待所有线程结束
        for (Thread t : students) {
            t.join();
        }

        // 最终查询课程剩余名额
        System.out.println("\n最终课程状态: " + manager.getCourse("CS101"));
    }
}
